<template>
  <div class="main">
    <div class="title"><span class="span">丨</span>商品管理</div><hr>
    <div class="header">
      <el-button class="button" @click="drawer=true">新增</el-button>
      <el-input class="search"
      :prefix-icon="Search"
      placeholder="请输入搜索名称"
      v-model="search"
      ></el-input>
    </div>
    <div class="table">
      <el-table :data="filterTableData" stripe height="100%">
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="batch" label="批次/个"></el-table-column>
        <el-table-column prop="type" label="类别"></el-table-column>
        <el-table-column prop="introduce" label="介绍"></el-table-column>
        <el-table-column prop="time" label="创建时间"></el-table-column>
        <el-table-column prop="" label="操作">
          <template #default="scope">
            <el-button link type="primary"  @click="deleteData(scope.$index)">
            删除
            </el-button>
            <el-button link type="primary"  @click="
            drawer2=true;
            changeIndex=scope.$index;
            originData={...tableData[scope.$index]};
            for(const key in newData) newData[key]=originData[key];
            ">
            编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-drawer
    v-model="drawer"
    title="新增商品"
  >
  <span>名称</span><el-input v-model="insertData.name"></el-input>
  <span>批次/个</span><el-input v-model="insertData.batch"></el-input>
  <span>类型</span><el-input v-model="insertData.type"></el-input>
  <span>介绍</span><el-input v-model="insertData.introduce"></el-input>
  <template #footer>
      <div>
        <el-button type="primary" @click="addData" class="button">增加</el-button>
        <el-button @click="drawer=false" class="button">取消</el-button>
      </div>
    </template>
  </el-drawer>

  <el-drawer
    v-model="drawer2"
    title="编辑商品"
  >
  <span>名称</span><el-input v-model="newData.name" :placeholder="originData.name"></el-input>
  <span>批次/个</span><el-input v-model="newData.batch" :placeholder="originData.batch"></el-input>
  <span>类型</span><el-input v-model="newData.type" :placeholder="originData.type"></el-input>
  <span>介绍</span><el-input v-model="newData.introduce" :placeholder="originData.introduce"></el-input>
  <template #footer>
      <div>
        <el-button type="primary" @click="changeData" class="button">修改</el-button>
        <el-button @click="drawer2=false" class="button">取消</el-button>
      </div>
    </template>
  </el-drawer>

  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue';
import {computed,ref,reactive} from 'vue'
const tableData = reactive([{
  name:'名字1',
  batch:1,
  type:'类型1',
  introduce:'描述111',
  time:'2024.10.9'
},
{
  name:'名字2',
  batch:2,
  type:'类型2',
  introduce:'描述222',
  time:'2024.10.9'
},
{
  name:'名字3',
  batch:3,
  type:'类型3',
  introduce:'描述333',
  time:'2024.10.9'
}])
const search = ref('')
const drawer = ref(false)
const drawer2 = ref(false)
const changeIndex = ref('')
const insertData = reactive({
  name:'',
  batch:'',
  type:'',
  introduce:'',
  time:''
})

const originData = reactive({
  name:'',
  batch:'',
  type:'',
  introduce:'',
  time:''
})

const newData = reactive({
  name:'',
  batch:'',
  type:'',
  introduce:'',
  time:''
})


const deleteData=(index)=>{
  tableData.splice(index, 1)
}

const filterTableData = computed(()=>
   tableData.filter(data=>!search.value||data.name.includes(search.value)
  )
)
const addData = () =>{
  const today = new Date();
  const year = today.getFullYear();  
  const month = today.getMonth() + 1; 
  const day = today.getDate();       
  insertData.time=year+'.'+month+'.'+day
  tableData.push({...insertData})
  for(const key in insertData){
    insertData[key]=''
  }
  drawer.value=false
}
const changeData = () =>{

  for(const key in newData){
    if(newData[key]===''){
      newData[key]=originData[key]
    }
  }
  tableData.splice(changeIndex.value,1,{...newData})
  for(const key in newData){
    newData[key]=''
  }
  drawer2.value=false

}
</script>

<style scoped>
@import url('./Commodity.css');
</style>